<?php

namespace app\admin\logic;

use app\common\exception\LogicException;
use app\model\DiseaseCategory;

class DiseaseCategoryLogic
{
    /**
     * @Description 列表
     * @author: zj
     * @param array $param
     * @time 2024/03/28 15:40:41
     * @return mixed
     */
    public function getList(array $param)
    {
        $model = new DiseaseCategory();
        $query = $model
            ->field('id,name,create_time')
            ->where('delete_time',$model::IS_DELETE_NO);
        if ($param['name']) {
            $query->where('name','like','%'.trim($param['name']).'%');
        }

        return $query->order('id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * @Description 新增
     * @author: zj
     * @param array $param
     * @time 2024/03/28 15:48:31
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(array $param)
    {
        $model = new DiseaseCategory();

        $exist = $model->checkNameIsExist($param['name']);
        if ($exist) {
            throw new LogicException('分类已存在');
        }

        $model->insert([
            'name' => $param['name'],
            'create_time' => request()->time(),
            'update_time' => request()->time()
        ]);

        return true;
    }

    /**
     * @Description 删除
     * @author: zj
     * @param int $id
     * @time 2024/03/28 15:53:57
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del(int $id)
    {
        $model = new DiseaseCategory();

        $exist = $model->checkIsExist($id);
        if (!$exist) {
            throw new LogicException('数据不存在');
        }
        
        $model->where('id',$id)
            ->update([
                'delete_time' => request()->time()
            ]);
        
        return true;
    }
}